#include "Camera.h"
#include "Constants.h"

// Constructor
Minicraft::Camera::GameCamera::GameCamera(LPDIRECT3DDEVICE9 d3ddevice)
{
	this->d3ddevice = d3ddevice;

	this->cameraPosition = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	this->lookPosition = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	this->cameraRotation = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	this->viewMatrix = new D3DXMATRIX();
	D3DXMatrixIdentity(this->viewMatrix);

	this->projectionMatrix = new D3DXMATRIX();
	D3DXMatrixIdentity(this->projectionMatrix);
}

Minicraft::Camera::GameCamera::~GameCamera()
{
	delete this->cameraPosition;
	delete this->lookPosition;
	delete this->cameraRotation;

	delete this->viewMatrix;
	delete this->projectionMatrix;
}

void Minicraft::Camera::GameCamera::Update()
{
D3DXMatrixIdentity(this->viewMatrix);
D3DXMatrixIdentity(this->projectionMatrix);

	D3DXVECTOR3* cameraPosition = this->cameraPosition;
	D3DXVECTOR3* lookPosition = this->lookPosition;

	//D3DXMATRIX cameraTransform;
	//D3DXMatrixIdentity(&cameraTransform);
	//D3DXMatrixTranslation(&cameraTransform, cameraPosition->x, cameraPosition->y, cameraPosition->z);
/*
	D3DXMATRIX cameraRotate;
	D3DXMatrixIdentity(&cameraRotate);
	D3DXMatrixRotationYawPitchRoll(&cameraRotate,
		D3DXToRadian(this->cameraRotation->y),
		D3DXToRadian(this->cameraRotation->x),
		D3DXToRadian(this->cameraRotation->z));

	D3DXMatrixMultiply(this->viewMatrix, this->viewMatrix, &cameraRotate);
	D3DXMatrixMultiply(this->viewMatrix, this->viewMatrix, &cameraTransform);*/
/*
	lookPosition->x = lookPosition->x + cameraPosition->x;
	lookPosition->y = cameraPosition->y;
	lookPosition->z = -(lookPosition->z + cameraPosition->z);
*/
	D3DXMatrixLookAtLH(this->viewMatrix,
		this->cameraPosition,
		this->lookPosition,
		&Minicraft::Constants::Camera::upPosition);

	

	this->d3ddevice->SetTransform(D3DTS_VIEW, this->viewMatrix);

	D3DXMatrixPerspectiveFovLH(this->projectionMatrix,
		D3DXToRadian(Minicraft::Constants::Camera::FOV),
		(FLOAT)Minicraft::Constants::SCREEN_WIDTH / (FLOAT)Minicraft::Constants::SCREEN_HEIGHT,
		Minicraft::Constants::Camera::NEAR_VIEW_PLANE,
		Minicraft::Constants::Camera::FAR_VIEW_PLANE);

	this->d3ddevice->SetTransform(D3DTS_PROJECTION, this->projectionMatrix);
}

D3DXVECTOR3* Minicraft::Camera::GameCamera::GetCameraPosition()
{
	return this->cameraPosition;
}

D3DXVECTOR3* Minicraft::Camera::GameCamera::GetCameraRotation()
{
	return this->cameraRotation;
}

D3DXVECTOR3* Minicraft::Camera::GameCamera::GetLookPosition()
{
	return this->lookPosition;
}